package com.dazhi.show.core;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//@Api(tags = "BaseController")
@SuppressWarnings("ALL")
public class BaseController<M extends IService<T>, T> {
    protected Logger logger = LoggerFactory.getLogger (BaseController.class);

    @Autowired
    private M baseService;

    @GetMapping
    public Result<Page<T>> list(Integer current, Integer size) {
        Page<T> page = new Page<>(current, size);
        page.addOrder(OrderItem.desc("id"));
        Page<T> searchPage = baseService.page(page);
        return new Result<Page<T>>(200,"success",searchPage);
    }

    @GetMapping("/{id}")
    public Result<T> info(@PathVariable(name="id") Integer id) {
        T T = baseService.getById(id);
        return new Result<T>(200, "success", T);
    }

    @PostMapping
    public Result saveOrUpdate(@RequestBody T T) {
        boolean save = baseService.saveOrUpdate(T);
        return new Result(200, "success");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(name="id") Integer id){
        boolean remove = baseService.removeById(id);
        return new Result(200, "success");
    }
}
